	
	package servlet;
	
	import java.io.IOException;
	import java.util.ArrayList;
	import java.util.List;
	import javax.servlet.ServletException;
	import javax.servlet.http.HttpServlet;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	import service.UserManager;
	import enitys.User;
	
	public class RegisterServlet extends HttpServlet {
	
		private static final long serialVersionUID = 1L;
	
		/**
		 * @author dengqin.
		 */
		public RegisterServlet() {
			super();
		}
	
		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
	
			doPost(request, response);
		}
	
		/**
		 * 
		 * 
		 * Register Srervlet
		 * 
		 * @param request
		 *            the request send by the client to the server
		 * @param response
		 *            the response send by the server to the client
		 * @throws ServletException
		 *             if an error occurred
		 * @throws IOException
		 *             if an error occurred
		 */
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String rname = request.getParameter("regname");
			String rpass = request.getParameter("regpass");
			String remail = request.getParameter("email");
			String rclass = request.getParameter("class");
			List<String> all = new ArrayList<String>();// 保存返回的信息
			User user = new User();
			user.setUsername(rname);
			user.setPassword(rpass);
			user.setEmail(remail);
			user.setClasses(rclass);
			boolean flag = UserManager.getInstance().register(user);
			if (!flag) {
				all.add("该用户名已经备注过了！请使用其他的用户名！");
			} else {
				all.add("用户注册成功！快去登录吧！");
			}
			request.setAttribute("all", all);// 保存错误信息
			request.getRequestDispatcher("/index.jsp").forward(request, response);
	
		}
	
	}
